﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace yysAuxiliary
{
    public class ConfigData
    {
        #region 需要存的数据

        /// <summary>
        /// 循环
        /// </summary>
        public int Loop { get; set; } = 1;

        /// <summary>
        /// 授权码
        /// </summary>
        public string AuthCode { get; set; }

        /// <summary>
        /// 模拟器类型
        /// </summary>
        public SimulatorType Type { get; set; } = SimulatorType.Default;

        /// <summary>
        /// 模拟器大小
        /// </summary>
        public Size SimulatorSize { get; set; } = new Size(1152, 679);

        /// <summary>
        /// 模拟器偏移高度
        /// </summary>
        public int Offsetheight { get; set; } = -31;
        #endregion

        #region 数据准备

        private static bool isInit = false;
        private static object o = new object();
        private ConfigData()
        {
            if (!isInit)
            {
                isInit = true;
                ReadJson();
            }
        }

        private static ConfigData _current;
        public static ConfigData Current
        {
            get
            {
                if (_current == null)
                    lock (o)
                    {
                        if (_current == null)
                            new ConfigData();
                    }

                return _current;
            }
        }


        private readonly static string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");

        private void ReadJson()
        {
            try
            {
                if (File.Exists(filepath))
                {
                    using (StreamReader streamReader = new StreamReader(filepath))
                    {
                        string jsonStr = streamReader.ReadToEnd();
                        ConfigData jsonObj = JsonConvert.DeserializeObject<ConfigData>(jsonStr);
                        _current = jsonObj ?? new ConfigData();
                    }
                }
                else
                {
                    _current = new ConfigData();
                    WriteJson();
                }

            }
            catch (Exception e)
            {

            }
        }

        /// <summary>
        /// 写数据到json中
        /// </summary>
        public void WriteJson()
        {
            try
            {
                File.WriteAllText(filepath, JsonConvert.SerializeObject(ConfigData._current));

            }
            catch (Exception e)
            {

            }
        }

        #endregion
    }
}
